package com.lijiajia.cloud.common.security.authentication;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;

/**
 * description
 *
 * @author lijiajia
 * @since 2019-05-31
 */
@Slf4j
@Configuration
public class AuthenticationSecurityConfiguration {

    /**
     * 认证 事件监听
     */
    @EventListener({AbstractAuthenticationEvent.class})
    public void authenticationLogEventListener(AbstractAuthenticationEvent event) {
        if (event instanceof AuthenticationSuccessEvent || event instanceof InteractiveAuthenticationSuccessEvent) {
            log.debug("event auth success: [authentication: {}]", event);
        } else if (event instanceof AbstractAuthenticationFailureEvent) {
            AbstractAuthenticationFailureEvent failureEvent = (AbstractAuthenticationFailureEvent) event;
            log.debug("event auth fail: [authentication:{}, exception:{}]", failureEvent.getAuthentication(), failureEvent.getException());
        } else {
            log.debug("event auth:{}", event);
        }

    }
}
